//
//  CardDetailsView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class CardDetailsView: UIView {
    
    var model : CardDetailsModel! {
        didSet{
            
            updateData()
        }
    }
    
    let titleLabel = UILabel()
    let imageView = UIImageView()
    let typeLabel = UILabel()
    let detailsLabel = UILabel()
    let similarLabel = UILabel()
    let detailsButton = UIButton()
    let freeButton = UIButton()
    let backButton = UIButton()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        
        titleLabel.text = notData
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        self.addSubview(titleLabel)
        
        imageView.image = UIImage.imageWithName("loading")
        self.addSubview(imageView)
        
        let recommendLabel = UILabel()
        recommendLabel.text = reasons + ":"
        recommendLabel.textColor = UIColor.lightGray
        recommendLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 15))
        self.addSubview(recommendLabel)
        
        typeLabel.text = notData
        typeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(typeLabel)
        
        detailsLabel.text = notData
        detailsLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(detailsLabel)
        
        similarLabel.text = notData
        similarLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(similarLabel)
        
        detailsButton.setTitle("[" + touchView + "]", for: .normal)
        detailsButton.setTitleColor(UIColor.orange, for: .normal)
        detailsButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(detailsButton)
        
        freeButton.setTitle(freeApplication, for: .normal)
        freeButton.backgroundColor = UIColor.cyanGreen()
        freeButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 15))
        self.addSubview(freeButton)
        
        backButton.setTitle(recalculate, for: .normal)
        backButton.backgroundColor = UIColor.cyanGreen()
        backButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 15))
        self.addSubview(backButton)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.centerX.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        imageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 200))
        }
        
        recommendLabel.snp.makeConstraints { (make) in
            make.top.equalTo(imageView.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.left.equalTo(imageView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        typeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(recommendLabel)
            make.top.equalTo(recommendLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.right.lessThanOrEqualTo(RATIO(maxNum: -20))
            make.height.greaterThanOrEqualTo(10)
        }
        
        detailsLabel.snp.makeConstraints { (make) in
            make.left.equalTo(typeLabel)
            make.top.equalTo(typeLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.right.lessThanOrEqualTo(RATIO(maxNum: 130))
            make.height.greaterThanOrEqualTo(10)
        }
        
        detailsButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(detailsLabel)
            make.left.equalTo(detailsLabel.snp.right)
            make.height.greaterThanOrEqualTo(10)
            make.width.equalTo(RATIO(maxNum: 80))
        }
        
        similarLabel.snp.makeConstraints { (make) in
            make.top.equalTo(detailsLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.equalTo(detailsLabel)
            make.right.lessThanOrEqualTo(RATIO(maxNum: -20))
            make.height.greaterThanOrEqualTo(10)
        }
        
        freeButton.snp.makeConstraints { (make) in
            make.right.equalTo(-(ScreenW/2 + RATIO(maxNum: 10)))
            make.top.equalTo(similarLabel.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.width.equalTo(RATIO(maxNum: 80))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        backButton.snp.makeConstraints { (make) in
            make.left.equalTo(ScreenW/2 + RATIO(maxNum: 10))
            make.top.equalTo(freeButton)
            make.width.equalTo(freeButton)
            make.height.equalTo(freeButton)
        }
        
    }
    
    //MARK: 更新数据
    fileprivate func updateData() {
        
        titleLabel.text = model.credit_name
        imageView.setUrlImage(with: kmHttps + model.image!, placeholder: UIImage.imageWithName("loading"))
        detailsLabel.text = model.youhui1
        similarLabel.text = suitability + "：" + model.snum! + "%"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
